﻿using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.IO.Ports;

namespace pandaSerial
{
    public class Program
    {
        readonly static SerialPort _serialPort = new SerialPort("COM1", 300) { ReadTimeout = 0 };

        readonly static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
        static string[] recv;                           // Used to parse the message
        static byte[] MessageBuffer = new byte[300];    // Used to build the message
        static byte[] serialBuffer = new byte[300];     // Used to receive the bytes
        static int readcount = 0;               // Tracks the count of bytes being received through the serial interface.
        static int mbFillLevel = 0;             // Running total of the current fill level of our message buffer.
        static int Newcount = 0;                // Tracks the new count of bytes received to update mbFillLevel
        static int iLocation = 0;               // Tracks the location for array insertion in ReadSerial

 

        public static void Main()
        {
            byte[] yx_data2 = System.Text.UTF8Encoding.UTF8.GetBytes("Message|Y - Pressed");
            byte[] qx_data2 = System.Text.UTF8Encoding.UTF8.GetBytes("Message|Q - Pressed");
            
            _serialPort.Open();
            _serialPort.Flush();
            while (true)
            {
                Thread.Sleep(10);
                ReadSerial();
                if (mbFillLevel == 1)
                {
                    switch (MessageBuffer[0])
                    {
                        case 81:
                            _serialPort.Write(qx_data2, 0, qx_data2.Length);
                            //Debug.Print("Wrote Q Response");
                            led.Write(false);
                            mbFillLevel = 0;
                            break;
                        case 89:
                            _serialPort.Write(yx_data2, 0, yx_data2.Length);
                            //Debug.Print("Wrote Y Response");
                            led.Write(false);
                            mbFillLevel = 0;
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        static void ReadSerial()
        {
            readcount = 0;
            Newcount = 0;
            iLocation = mbFillLevel;
            readcount = _serialPort.BytesToRead;
            _serialPort.Read(serialBuffer, 0, serialBuffer.Length);

            if (readcount > 0)
            {
                Newcount = mbFillLevel + readcount;
                if (readcount >= 1)
                {
                    mbFillLevel = Newcount;
                    Array.Copy(serialBuffer, 0, MessageBuffer, iLocation, readcount);
                    led.Write(true);
                    //Debug.Print("Received a response");
                }
            }
        }

    }
    
}
